//
//  GNStatusOriginalView.swift
//  SinaWeChat
//
//  Created by mac on 15/12/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
class GNStatusOriginalView: UIView {
    
    //当前视图底部约束
    var bottomContraint : Constraint?
    
    var statusViewModel:GNStatusViewModel?{
        
        didSet{
            
            //设置头像
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            
            //设置昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            
            // 设置创建时间,先写死,后面会处理
            timeLabel.text = statusViewModel?.sourceText
            
            sourceLabel.text = statusViewModel?.creatAtText
            
            //设置会员图标以及认证
            vipImageView.image = statusViewModel?.vipImage
            
            verifiedImageView.image = statusViewModel?.verifiedImage
            
            //设置原创微博的内容
            contentLabel.text = statusViewModel?.status?.text
            
            //设置原创微博的配图
            
            bottomContraint?.uninstall()
            
            //如果有配图 1.设置数据,2.显示控件,3.更新约束
            if let pic_urls =  statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                
                //设置数据
                pictureView.pic_urls = pic_urls
                
                //显示配图
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomContraint = make.bottom.equalTo(pictureView).offset(GNStatusCellMargin).constraint
                    
                })
                
            }else{
                pictureView.hidden = true
                
                //跟新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomContraint = make.bottom.equalTo(contentLabel).offset(GNStatusCellMargin).constraint
                })
            }
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        //1.添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //2.添加约束
        
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(GNStatusCellMargin)
            make.leading.equalTo(self).offset(GNStatusCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
            
        }
        
        //昵称
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(GNStatusCellMargin)
        }
        
        //会员图标
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(GNStatusCellMargin)
        }
        
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(GNStatusCellMargin)
        }
        
        //认证
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        //原创微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            
            make.top.equalTo(iconView.snp_bottom).offset(GNStatusCellMargin)
        }
        
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(GNStatusCellMargin)
            
        }
        
        //关键
        snp_makeConstraints { (make) -> Void in
            
            
         self.bottomContraint = make.bottom.equalTo(pictureView.snp_bottom).offset(GNStatusCellMargin).constraint
        }
    }
    
    //MARK:- 懒加载控件
    //头像
    private lazy var iconView: UIImageView = UIImageView(image:UIImage(named: "avatar_default_big"))
    
    //昵称label
    private lazy var nameLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    
    //时间
    
    private lazy var timeLabel:UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 10)
    
    //来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 10)
    
    //会员图标
    private lazy var vipImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //认证
    private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博内容
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - 2*GNStatusCellMargin)
    
    //初始化一个原创微博的配图控件
    private lazy var pictureView:GNStatusPictureView = {
        
        let view = GNStatusPictureView()
        view.backgroundColor = self.backgroundColor
    
        return view
    }()
    
}
